package com.farm.kaiping.controller;


import com.farm.kaiping.entity.SysMenu;
import com.farm.kaiping.po.SysMenuPO;
import com.farm.kaiping.service.SysMenuService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farm.common.util.PageParaUtils;
import com.farm.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单权限表(SysMenu)表控制层
 *
 * @author zhuhui
 * @since 2024-11-26 22:58:21
 */
@RestController
@RequestMapping("/sysMenu")
@AllArgsConstructor
@Api(tags = "菜单权限表控制器")
public class SysMenuController {

	private final SysMenuService sysMenuService;

	@ApiOperation(value = "分页查询菜单权限表", notes = "权限：view_sysMenu_page")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysMenu_page')")
	@PostMapping("/page")
	public R<Page<SysMenu>> getSysMenuPage(@RequestBody SysMenuPO sysMenuPO) {
		try{
		Page page = PageParaUtils.withdrawPageByParamType(sysMenuPO);
		return R.ok(sysMenuService.getSysMenuPage(page, sysMenuPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询菜单权限表列表", notes = "权限：view_sysMenu_list")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysMenu_list')")
	@PostMapping("/list")
	public R<List<SysMenu>> getSysMenuList(@RequestBody SysMenuPO sysMenuPO) {
		try{
			return R.ok(sysMenuService.getSysMenuList(sysMenuPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "通过id查询菜单权限表", notes = "权限 view_sysMenu_view")
	@GetMapping("/{id}" )
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysMenu_view')" )
	public R getById(@PathVariable("id" ) String id) {
		try{
			return R.ok(sysMenuService.getById(id));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询一条菜单权限表", notes = "权限：view_sysMenu_one")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysMenu_one')")
	@PostMapping("/one")
	public R<SysMenu> getSysMenu(@RequestBody SysMenuPO sysMenuPO) {
		try{
			return R.ok(sysMenuService.getSysMenu(sysMenuPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "新增菜单权限表", notes = "权限：view_sysMenu_add")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysMenu_add')")
	@PostMapping("/add")
	public R saveSysMenu(@RequestBody SysMenuPO sysMenuPO) {
		try{
		    return R.ok(sysMenuService.saveSysMenu(sysMenuPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "修改菜单权限表", notes = "权限：view_sysMenu_edit")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysMenu_edit')")
	@PostMapping("/edit")
	public R updateSysMenu(@RequestBody SysMenuPO sysMenuPO) {
		try{
		    return R.ok(sysMenuService.updateSysMenu(sysMenuPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}    
	}

	@ApiOperation(value = "通过id删除菜单权限表", notes = "权限：view_sysMenu_del")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysMenu_del')")
	@PostMapping("/del")
	public R removeSysMenu(@RequestBody SysMenuPO sysMenuPO) {
		try{
		    return R.ok(sysMenuService.removeSysMenu(sysMenuPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}  
	}

	@ApiOperation(value = "根据条件获取菜单权限树形结构列表", notes = "权限：view_sysMenu_tree")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysMenu_tree')")
	@PostMapping("/tree")
	public R treeSysMenu(@RequestBody SysMenuPO sysMenuPO) {
		try{
			return R.ok(sysMenuService.treeSysMenu(sysMenuPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
}

